package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjOmnipotentRequiresBo;
import com.ruoyi.zhgd.domain.vo.ZjOmnipotentRequiresVo;
import com.ruoyi.zhgd.domain.ZjOmnipotentRequires;
import com.ruoyi.zhgd.mapper.ZjOmnipotentRequiresMapper;
import com.ruoyi.zhgd.service.IZjOmnipotentRequiresService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 万能机技术要求Service业务层处理
 *
 * @author cj
 * @date 2023-11-14
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjOmnipotentRequiresServiceImpl  implements IZjOmnipotentRequiresService {

    private final ZjOmnipotentRequiresMapper baseMapper;

    /**
     * 查询万能机技术要求
     */
    @Override
    public ZjOmnipotentRequiresVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机技术要求列表
     */
    @Override
    public TableDataInfo<ZjOmnipotentRequiresVo> queryPageList(ZjOmnipotentRequiresBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjOmnipotentRequires> lqw = buildQueryWrapper(bo);
        Page<ZjOmnipotentRequiresVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机技术要求列表
     */
    @Override
    public List<ZjOmnipotentRequiresVo> queryList(ZjOmnipotentRequiresBo bo) {
        LambdaQueryWrapper<ZjOmnipotentRequires> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjOmnipotentRequires> buildQueryWrapper(ZjOmnipotentRequiresBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjOmnipotentRequires> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getSymc()), ZjOmnipotentRequires::getSymc, bo.getSymc());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), ZjOmnipotentRequires::getMark, bo.getMark());
        lqw.eq(bo.getRm() != null, ZjOmnipotentRequires::getRm, bo.getRm());
        lqw.eq(bo.getRel() != null, ZjOmnipotentRequires::getRel, bo.getRel());
        lqw.eq(bo.getAgt() != null, ZjOmnipotentRequires::getAgt, bo.getAgt());
        lqw.eq(bo.getQu() != null, ZjOmnipotentRequires::getQu, bo.getQu());
        lqw.eq(bo.getQq() != null, ZjOmnipotentRequires::getQq, bo.getQq());
        return lqw;
    }

    /**
     * 新增万能机技术要求
     */
    @Override
    public R insertByBo(ZjOmnipotentRequiresBo bo) {
        ZjOmnipotentRequires add = BeanUtil.toBean(bo, ZjOmnipotentRequires.class);
        validEntityBeforeSave(add);
        List<Map> list = baseMapper.selectSymc(bo.getSymc());
        if (list.size() > 0) {
            return R.warn("技术要求已存在，禁止重复添加！");
        }
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return R.ok(flag);
    }

    /**
     * 修改万能机技术要求
     */
    @Override
    public R updateByBo(ZjOmnipotentRequiresBo bo) {
        ZjOmnipotentRequires update = BeanUtil.toBean(bo, ZjOmnipotentRequires.class);
        validEntityBeforeSave(update);
        List<Map> list = baseMapper.selectUpList(bo);
        if (list.size() > 0) {
            return R.warn("技术要求已存在，禁止修改！");
        }
        boolean flag = baseMapper.update(null, new UpdateWrapper<ZjOmnipotentRequires>().lambda()
            .set(ZjOmnipotentRequires::getQu, bo.getQu())
            .set(ZjOmnipotentRequires::getQq, bo.getQq())
            .set(ZjOmnipotentRequires::getAgt, bo.getAgt())
            .set(ZjOmnipotentRequires::getRel, bo.getRel())
            .set(ZjOmnipotentRequires::getRm, bo.getRm())
            .set(ZjOmnipotentRequires::getSymc, bo.getSymc())
            .set(ZjOmnipotentRequires::getMark, bo.getMark())
            .set(ZjOmnipotentRequires::getLogic, bo.getLogic())
            .eq(ZjOmnipotentRequires::getId, bo.getId())
        ) > 0;
        return R.ok(flag);
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjOmnipotentRequires entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机技术要求
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ZjOmnipotentRequires selectRequires(String symc) {
        return baseMapper.selectRequires(symc);
    }
}
